﻿Imports System.Data

Public Class CheckBoxList_Filter
    Public DataSource As DataTable = Nothing
    Public dtRelationDataSource As DataTable = Nothing

    Public ParentIDMember As String = Nothing
    Public ChildIDMember As String = Nothing

    Public DisplayMember As String = Nothing
    Public ValueMember As String = Nothing
    'Public ParentValueMember As String = Nothing

    Public Filter As String = String.Empty
    Public Event FilterChanged(ByVal Filter As String)
    Private HoldEvents As Boolean = True

    Function BindData()
        If IsNothing(DataSource) Then _
            Throw New ArgumentException("DataSource: must be set ")
        If IsNothing(DisplayMember) Then _
                Throw New ArgumentException("DisplayMember: must be set ")
        If IsNothing(ValueMember) Then _
                Throw New ArgumentException("ValueMember: must be set ")
        Try
            Dim iTop As Integer = 0
            HoldEvents = True
            pnlSlider.Controls.Clear()

            For Each dr As DataRow In DataSource.Rows
                Dim chkItem As New CheckBox
                chkItem.Parent = pnlSlider
                chkItem.Text = dr.Item(DisplayMember)
                chkItem.Tag = dr.Item(ValueMember)
                chkItem.Top = iTop
                chkItem.Visible = True
                'chkItem.Checked = True
                iTop += 20
                AddHandler chkItem.CheckStateChanged, AddressOf chkItem_CheckStateChanged
                If pnlSlider.Height <= iTop + 30 Then
                    pnlSlider.Height = iTop + 30
                End If
            Next
            If pnlSlider.Height > pnlMain.Height Then
                VScrollBar1.Maximum = pnlSlider.Height / 2
                VScrollBar1.Enabled = True
            Else
                VScrollBar1.Enabled = False
            End If
            HoldEvents = False
            CheckCheck()
            Return True

        Catch ex As Exception
            Throw ex
        End Try

    End Function

    Sub CheckCheck()
        Filter = String.Empty
        For Each ctrl As Control In pnlSlider.Controls
            If TypeOf ctrl Is CheckBox Then
                Dim lctr As CheckBox = ctrl
                If lctr.Checked Then
                    Filter &= ValueMember & " =" & ctrl.Tag & " or "
                End If
            End If
        Next
        If Not Filter = String.Empty Then
            Filter = Filter.Substring(0, Filter.Length - 4)
            Filter = " ( " & Filter & " ) "
        End If

        If Filter <> String.Empty Then
            dtRelationDataSource.DefaultView.RowFilter = Filter
            Filter = String.Empty
            If dtRelationDataSource.DefaultView.Count > 0 Then
                For iRow As Integer = 0 To dtRelationDataSource.DefaultView.Count - 1
                    Filter &= ParentIDMember & " =" & dtRelationDataSource.DefaultView(iRow)(ParentIDMember) & " or "
                Next
            End If
            If Not Filter = String.Empty Then
                Filter = Filter.Substring(0, Filter.Length - 4)
                Filter = " ( " & Filter & " ) "
            End If
        End If
        
        RaiseEvent FilterChanged(Filter)

    End Sub

    Private Sub chkItem_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not HoldEvents Then _
            CheckCheck()
    End Sub

    Private Sub VScrollBar1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VScrollBar1.ValueChanged
        pnlSlider.Top = VScrollBar1.Value * -1
    End Sub
End Class
